//
// Created by lh on 17-8-30.
//

#include <iostream>
using namespace std;

int e[10][10];
int n,m;
int book[10];
int patch[10];
void dfs(int tar,int step,int cnt)
{
    if(tar==5)
    {
        for (int t = 0; t < step; ++t) {
            cout<<patch[t]<<" ";
            cout<<cnt<<endl;
        }
    }

    for (int t = 1; t <= n; ++t) {
        if(e[tar][t]&&!book[tar])
        {
            book[tar]==1;
            dfs(t,step+1,cnt+e[tar][t]);
            book[tar]=0;
        }
    }
    return;
}

int main()
{
    for (int i = 1; i <=n; ++i) {
        for (int j = 1; j <= n; ++j) {
            if(i==j)
                e[i][j]=0;
            else
                e[i][j]=99999;
        }
    }
    cin>>n>>m;
    for (int k = 1; k <= m; ++k) {
            int x,y,l;//l:length
            cin>>x>>y>>l;
            e[x][y]=l;
    }
    book[1]=1;
    dfs(1,1,0);
    return 0;
}